package com.ruoyi.swla2.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.swla2.domain.TerminalsXb;
import com.ruoyi.swla2.service.ITerminalsXbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 户表档案Controller
 *
 * @author gdqt
 * @date 2023-06-16
 */
@RestController
@RequestMapping("/device/terminalsXbInfo")
public class TerminalsXbController extends BaseController {
    @Autowired
    private ITerminalsXbService terminalsXbService;

    /**
     * 查询户表档案列表
     */
    @PreAuthorize("@ss.hasPermi('device:terminalsXbInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(TerminalsXb terminalsXb) {
        startPage();
        List<TerminalsXb> list = terminalsXbService.selectTerminalsXbList(terminalsXb);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('device:terminalsXbInfo:list')")
    @GetMapping("/getBoundTerminalsXb")
    public TableDataInfo getBoundTerminalsXb(TerminalsXb terminalsXb) {
        startPage();
        List<TerminalsXb> list = terminalsXbService.selectTerminalsXbList(terminalsXb);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('device:terminalsInfo:list')")
    @GetMapping("/getUnbindXbTerminals")
    public TableDataInfo getUnbindXbTerminals(TerminalsXb terminalsXb) {
        startPage();
        List<TerminalsXb> list = terminalsXbService.getUnbindXbTerminals(terminalsXb);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('device:terminalsInfo:list')")
    @PostMapping("/batchBindTerminalsXbInfo")
    public AjaxResult batchBindTerminalsXbInfo(@RequestBody Map<String, String> paramMap) {
        String[] ids = paramMap.get("ids").split(",");
        String areaId = paramMap.get("areaId");
        for (String id : ids) {
            TerminalsXb terminalsXb = terminalsXbService.selectTerminalsXbByID(Long.valueOf(id));
            if (terminalsXb != null) {
                terminalsXb.setAreaID(areaId);
                terminalsXbService.updateTerminalsXb(terminalsXb);
            }
        }
        return success();
    }

    @PreAuthorize("@ss.hasPermi('device:terminalsInfo:list')")
    @PostMapping("/batchUnbindTerminalsXbInfo")
    public AjaxResult batchUnbindTerminalsXbInfo(@RequestBody Map<String, String> paramMap) {
        String[] ids = paramMap.get("ids").split(",");
        for (String id : ids) {
            TerminalsXb terminalsXb = terminalsXbService.selectTerminalsXbByID(Long.valueOf(id));
            if (terminalsXb != null) {
                terminalsXb.setAreaID(null);
                terminalsXbService.updateTerminalsXb(terminalsXb);
            }
        }
        return success();
    }

    /**
     * 导出户表档案列表
     */
    @PreAuthorize("@ss.hasPermi('device:terminalsXbInfo:export')")
    @Log(title = "户表档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TerminalsXb terminalsXb) {
        List<TerminalsXb> list = terminalsXbService.selectTerminalsXbList(terminalsXb);
        ExcelUtil<TerminalsXb> util = new ExcelUtil<TerminalsXb>(TerminalsXb.class);
        util.exportExcel(response, list, "户表档案数据");
    }

    /**
     * 获取户表档案详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:terminalsXbInfo:query')")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID) {
        return success(terminalsXbService.selectTerminalsXbByID(ID));
    }

    /**
     * 新增户表档案
     */
    @PreAuthorize("@ss.hasPermi('device:terminalsXbInfo:add')")
    @Log(title = "户表档案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TerminalsXb terminalsXb) {
        terminalsXb.setTenantId(this.getTenantId());
        return toAjax(terminalsXbService.insertTerminalsXb(terminalsXb));
    }

    /**
     * 修改户表档案
     */
    @PreAuthorize("@ss.hasPermi('device:terminalsXbInfo:edit')")
    @Log(title = "户表档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TerminalsXb terminalsXb) {
        return toAjax(terminalsXbService.updateTerminalsXb(terminalsXb));
    }

    /**
     * 删除户表档案
     */
    @PreAuthorize("@ss.hasPermi('device:terminalsXbInfo:remove')")
    @Log(title = "户表档案", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs) {
        return toAjax(terminalsXbService.deleteTerminalsXbByIDs(IDs));
    }

    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<TerminalsXb> util = new ExcelUtil<>(TerminalsXb.class);
        List<TerminalsXb> xbList = util.importExcel(file.getInputStream());
        String opName = getUsername();
        String tenantId = getTenantId();
        Map<String,Object> message = terminalsXbService.importTerminalsXb(xbList, updateSupport,tenantId, opName);
        return success(message);
    }

    //企业租号、分区编号、户表类型、用户编号、用户名称、用户地址、表编号、生产厂家（用数据字典，例如德高：1002）、口径
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<TerminalsXb> util = new ExcelUtil<>(TerminalsXb.class);
        util.importTemplateExcel(response, "户表数据");
    }
}

















